extends Control
 
var current_scene = null
var path

func _ready():
	hide()
	var root = get_tree().root
	current_scene = root.get_child(root.get_child_count() - 1)
	
# 进度条的跳转
func goto_scene_progress(lpath):
	call_deferred("_deferred_self_scene", lpath)

func _deferred_self_scene(lpath):
	show()
	if current_scene != null:
		current_scene.free()
	show_progress(lpath)
	
# 显示进度条
func show_progress(lpath):
	self.path = lpath
	ResourceLoader.load_threaded_request(lpath)
	$StatusTimer.start()
	
# 更新进度条
func _on_status_timer_timeout():
	var process_arr = []
	var status = ResourceLoader.load_threaded_get_status(path,process_arr)
	$ProgressBar.value = int(process_arr[0]*100)
	if status == 3:
		# 完成时处理
		$ProgressBar.value = 100
		$StatusTimer.stop()
		goto_scene(path, true)

# 跳转页面
func goto_scene(lpath, is_async=false):
	# This function will usually be called from a signal callback,
	# or some other function in the current scene.
	# Deleting the current scene at this point is
	# a bad idea, because it may still be executing code.
	# This will result in a crash or unexpected behavior.

	# The solution is to defer the load to a later time, when
	# we can be sure that no code from the current scene is running:
	hide()
	call_deferred("_deferred_goto_scene", lpath, is_async)

	
func _deferred_goto_scene(lpath, is_async):
	# It is now safe to remove the current scene.
	if current_scene != null:
		current_scene.free()
	
	# Load the new scene.
	var s :Resource
	if is_async:
		s = ResourceLoader.load_threaded_get(lpath)
	else:
		s = ResourceLoader.load(lpath)

	# Instance the new scene.
	current_scene = s.instantiate()

	# Add it to the active scene, as child of root.
	get_tree().root.add_child(current_scene)

	# Optionally, to make it compatible with the SceneTree.change_scene_to_file() API.
	get_tree().current_scene = current_scene	
	 
	
 
